<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<title>倒数</title>
	<meta name="description" content="" />
	<link rel="stylesheet" type="text/css" href="css/global_121024_uncompress.css">
	<script type="text/javascript" src="js/jquery-1.7.js"></script>
</head>
<body>
	<h1>倒数</h1>
	<script type="text/javascript">
	/*
		** name:时间倒数效果
		** desc:当前时间到目标时间之间的剩余时间,以秒为单位倒数,剩余时间最大单位是天,最小单位是秒
		** @param {Date} 目标时间
		** @param {String} 显示剩余天数的元素
		** @param {String} 显示剩余小时数的元素
		** @param {String} 显示剩余分钟数的元素
		** @param {String} 显示剩余秒数的元素

	 */
	$.countDown = function(options){
		var defaults = {
			finalTime:,
			dayElem:,
			hourElem,
			minuteElem,
			secondElem,
		}
	}



	//不依赖jQuery版本
    var fireTime = function(finalTime, parentElem, dayElem, hourElem, minuteElem, secondElem, finishElem,leftDaysCallback) {
        this.finalTime = finalTime;
        this.parentElem = parentElem;
        this.dayElem = dayElem;
        this.hourElem = hourElem;
        this.minuteElem = minuteElem;
        this.secondElem = secondElem;
        this.finishElem = finishElem;
        this.leftDaysCallback = leftDaysCallback||false;
        this.getMilliSeconds();
    };
    fireTime.prototype = {
        getMilliSeconds: function() {
            var _t = this;
            var _finalTime = new Date(_t.finalTime);
            if (!_finalTime) return alert("时间格式有误");
            _t.leftMilliSeconds = new Date(_finalTime) - new Date();
            if (_t.leftMilliSeconds < 0) {
                _t.parentElem.style.display = "none";
                _t.finishElem.style.display = "";
                return;
            }
            _t.getLeftHMS();
        },
        getLeftHMS: function() {
            var _t = this;
            var _leftMilliSeconds = Math.floor(_t.leftMilliSeconds / 1000);
            _t.leftDays = Math.floor(_leftMilliSeconds / (60 * 60 * 24));
            if(_t.leftDays > 30 && this.leftDaysCallback) return this.leftDaysCallback(_t.parentElem);
            _leftMilliSeconds = _leftMilliSeconds - _t.leftDays * (60 * 60 * 24);
            _t.leftHours = Math.floor(_leftMilliSeconds / (60 * 60));
            _leftMilliSeconds = _leftMilliSeconds - _t.leftHours * (60 * 60);
            _t.leftMinutes = Math.floor(_leftMilliSeconds / 60);
            _t.leftSeconds = _leftMilliSeconds - _t.leftMinutes * 60;
            _t.dayElem.innerHTML = _t.leftDays;
            _t.hourElem.innerHTML = _t.leftHours;
            _t.minuteElem.innerHTML = _t.leftMinutes;
            _t.secondElem.innerHTML = _t.leftSeconds;
            // console.log(_t.leftDays+":"+_t.leftHours+":"+_t.leftMinutes+":"+_t.leftSeconds);
            _t.setSecondsElem();
        },
        setDayElem: function() {
            var _t = this;
            if (_t.leftDays > 0) {
                _t.leftDays--;
                // _t.leftHours = 23;
                // _t.leftMinutes = 59;
                // _t.leftSeconds = 59;
                _t.dayElem.innerHTML = _t.leftDays;
                // _t.hourElem.innerHTML = _t.leftHours;
                // _t.minuteElem.innerHTML = _t.leftMinutes;
                // _t.secondElem.innerHTML = _t.leftSeconds;
            }
        },
        setHourElem: function() {
            var _t = this;
            if (_t.leftHours == 0) {
                if (_t.leftDays > 0) {
                    _t.setDayElem();
                    // return;
                    _t.leftHours = 24;
                    // _t.leftMinutes = 60;
                    // _t.leftSeconds = 60;
                }
            }
            _t.leftHours--;
            _t.leftMinutes = 59;
            _t.leftSeconds = 59;
            _t.hourElem.innerHTML = _t.leftHours;
            _t.minuteElem.innerHTML = _t.leftMinutes;
            _t.secondElem.innerHTML = _t.leftSeconds;
        },
        setMinuteElem: function() {
            var _t = this;
            if (_t.leftMinutes == 0) {
                if (_t.leftHours > 0 || _t.leftDays > 0) {
                    _t.setHourElem();
                    _t.leftMinutes = 60;
                }
            }
            _t.leftMinutes--;
            _t.leftSeconds = 59;
            // _t.leftSeconds--;
            _t.minuteElem.innerHTML = _t.leftMinutes;
            _t.secondElem.innerHTML = _t.leftSeconds;
        },
        setSecondsElem: function() {
            var _t = this;
            _t.secondsInterval = setInterval(function() {
                if (_t.leftSeconds == 0) {

                    // if(_t.leftMinutes > 0){
                    // 	_t.setMinuteElem();
                    // 	_t.leftSeconds = 60;
                    // }else{
                    if (_t.leftDays == 0 && _t.leftHours == 0 && _t.leftMinutes == 0) {
                        clearInterval(_t.secondsInterval);
                        _t.parentElem.style.display = "none";
                        _t.finishElem.style.display = "";
                    }
                    _t.setMinuteElem();
                    return;
                    // }
                }
                _t.leftSeconds--;
                _t.secondElem.innerHTML = _t.leftSeconds;
            }, 1000)
        }
    }

	</script>
</body>
</html>